//
//  ChainCommenFIleItem.m
//  Chain
//
//  Created by zhaosh on 2018/4/4.
//  Copyright © 2018年 zhaosh. All rights reserved.
//

#import "ChainCommenFIleItem.h"
#import "FCFileManager.h"
#import "ChainDBManager.h"


@implementation ChainCommenFIleItem

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    return @{
             @"id_":@"id"
             };
}

-(void)setFilesize:(NSString *)filesize{
    
    
    _filesize = filesize;
    self.formatFilesize = [NSString fileSizeFormart:[NSNumber numberWithInteger:[filesize integerValue]]];
    //    self.formatFilesize = [FCFileManager sizeFormatted:[NSNumber numberWithInteger:[filesize integerValue]]];
}

-(void)setFilepath:(NSString *)filepath{
    if(filepath.length == 0){
        return;
    }
    self.fileAbsolutePath = filepath;
    _filepath = [FCFileManager pathForDocumentsDirectoryWithPath:filepath];
}
-(void)setAddtime:(NSString *)addtime
{
    self.originTime = addtime;//为了防止回存的时候不能正确取到时间戳
    _addtime = [self getDataStringFromTimestamp:addtime];
    
}
- (NSString *)getDataStringFromTimestamp:(NSString *)string
{
    // 转换为2015-11-11 13：00
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[string doubleValue]];
    // 时间添加八个小时
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:date] ;
    
    NSString *time = [NSString stringWithFormat:@"%@",destinationDateNow];
    
    return time;
}

-(NSDictionary *)getModelToDic{
    NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:[self yy_modelToJSONObject]];
    info[@"filepath"] = info[@"fileAbsolutePath"];
    info[@"addtime"] = self.originTime;
    [info removeObjectForKey:@"originTime"];
    [info removeObjectForKey:@"fileAbsolutePath"];
    [info removeObjectForKey:@"formatFilesize"];
    [info removeObjectForKey:@"id"];
    return info;
}

-(NSString *)filetype{
    if (!_filetype || _filetype.length == 0) {
        _filetype = [[ChainDBManager shareClient] getFileTypeByExtension:self.extension];
    }
    return _filetype;
}

@end
